package cn.pw.test.com.news.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.aries.ui.view.title.TitleBarView;

import java.util.List;

import cn.pw.test.com.news.R;
import cn.pw.test.com.news.adapter.VideoAdapter;
import cn.pw.test.com.news.bean.VideoBean;
import cn.pw.test.com.news.manager.NewsPathManger;
import cn.pw.test.com.news.server.NewsBack;
import cn.pw.test.com.news.server.NewsGlobal;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;
import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.manager.GlideManager;

@Route(path = NewsPathManger.NEWS_VIDEO_DETAIL)
public class VideoDetailsActivity extends BaseTitleActivity implements View.OnClickListener {

    private JCVideoPlayerStandard JcVideoPlayerView;
    private TextView videoTitleTx;
    private RecyclerView videoRecycler;
    private VideoAdapter videoAdapter;

    private VideoBean videoBean;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setBgDrawable(null)
                .setLeftTextDrawable(null)
                .setBgColor(Color.TRANSPARENT);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_video_details;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        JcVideoPlayerView = findViewById(R.id.video_player_view);
        videoRecycler = findViewById(R.id.video_recycler_view);
        videoTitleTx = findViewById(R.id.video_player_title);

        videoBean = (VideoBean) getIntent().getSerializableExtra("videoNews");

        videoAdapter = new VideoAdapter(R.layout.news_layout_item_video);
        videoRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        videoRecycler.setAdapter(videoAdapter);

        playerSelectVideo(videoBean);
        requestData();
    }

    private void playerSelectVideo(VideoBean videoBean) {
        if (videoBean == null)
            return;
        JcVideoPlayerView.setUp(videoBean.getUrl(), JCVideoPlayerStandard.SCREEN_LAYOUT_LIST, "");
        GlideManager.loadImg(videoBean.getUrl(), JcVideoPlayerView.thumbImageView);
        videoTitleTx.setText(videoBean.getTitle());
    }

    private void requestData() {
        NewsGlobal.getNewsImp().requestVideo(0, 10, new NewsBack<List<VideoBean>>() {
            @Override
            public void onBack(List<VideoBean> beans, String err) {
                if (beans != null) {
                    videoAdapter.replaceData(beans);
                    videoAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onBackPressed() {
        if (JcVideoPlayerView.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        JcVideoPlayerView.releaseAllVideos();
    }
}
